package com.czmh.etc.onlinepubsys.messagehandler;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.czmh.etc.onlinepubsys.constants.QrCodeConstants;
import com.czmh.etc.onlinepubsys.model.EtcUser;
import com.czmh.etc.onlinepubsys.services.EtcUserInfoSevice;
import com.czmh.etc.onlinepubsys.services.QrCodePromoterService;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;

@Slf4j
@Component
public class ScanEventMessageHander implements WxMpMessageHandler {
    @Value("${weburl}")
    private String weburl;
    @Autowired
    private EtcUserInfoSevice etcUserInfoSevice;
    @Autowired
    private QrCodePromoterService qrCodePromoterService;
	
	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage message, Map<String, Object> context, WxMpService wxMpService,
			WxSessionManager sessionManager) throws WxErrorException {
		WxMpXmlOutMessage reMsg = null;
		//发送者帐号
		String fromUser=message.getFromUser();
		//开发者微信号
		String touser=message.getToUser();
		// 事件KEY值
		String eventKey = message.getEventKey();
		String lang = "zh_CN"; //语言
		WxMpUser wpMpUser = wxMpService.getUserService().userInfo(fromUser,lang);
        // 1.保存关注用户信息
        EtcUser etcUser = etcUserInfoSevice.saveWechatInfo(wpMpUser);
		if (StringUtils.isNotEmpty(eventKey)) {
			log.info("已关注用户扫描带参数的二维码处理scene参数业务==========" + eventKey);
		    String scene = eventKey;
            if(StringUtils.isNotEmpty(scene) && scene.startsWith(QrCodeConstants.QR_LEVEL_1)){
                // 绑定二级关系，同时增加扫码记录,返回二级推广员二维码图片链接
                Map<String, String> map = qrCodePromoterService.bindPromoter(etcUser, scene);
                // 回复成为二级的文本消息
                String msg = "";
                if (StringUtils.isNotEmpty(map.get("isNew"))) {
                    msg = "您好，欢迎关注“易行易停”！\r\n您已成为手机用户："+ map.get("phone") + "的下级推广员\r\n\r\n" + "<a href=\""+ weburl + "/getQRcode?userId=" + etcUser.getUser_id() +"\">点击获取专属推广码</a>";
                } else {
                    msg = "您好，欢迎关注“易行易停”！\r\n\r\n" + "<a href=\""+ weburl + "/qrcode/getqrcode?userId=" + etcUser.getUser_id() +"\">点击查看您的推广码</a>";
                }
                reMsg = WxMpXmlOutMessage.TEXT()
                        .content(msg)
                        .fromUser(touser)
                        .toUser(fromUser)
                        .build();
            } else {
                // 增加扫码记录
                qrCodePromoterService.addQrScanRecord(etcUser, scene);
            }
		} 
		return reMsg;
	}
}